CC = gcc
CFLAGS = -Wall -Wextra -std=c99 -O2 -I./include
LDFLAGS = -lm

# 源文件
SRCDIR = src
INCDIR = include
TESTDIR = tests
BUILDDIR = build
DATADIR = data

SOURCES = $(wildcard $(SRCDIR)/*.c)
OBJECTS = $(SOURCES:$(SRCDIR)/%.c=$(BUILDDIR)/%.o)
TEST_SOURCES = $(wildcard $(TESTDIR)/*.c)
TEST_OBJECTS = $(TEST_SOURCES:$(TESTDIR)/%.c=$(BUILDDIR)/test_%.o)

# 目标文件
TARGET = $(BUILDDIR)/satellite_control
TEST_TARGET = $(BUILDDIR)/test_runner

# 默认目标
all: $(TARGET)

# 构建主程序
$(TARGET): $(OBJECTS) | $(BUILDDIR)
	$(CC) $(OBJECTS) -o $@ $(LDFLAGS)

# 构建测试程序
test: $(TEST_TARGET)
	$(TEST_TARGET)

$(TEST_TARGET): $(OBJECTS) $(TEST_OBJECTS) | $(BUILDDIR)
	$(CC) $(filter-out $(BUILDDIR)/main.o, $(OBJECTS)) $(TEST_OBJECTS) -o $@ $(LDFLAGS)

# 编译对象文件
$(BUILDDIR)/%.o: $(SRCDIR)/%.c | $(BUILDDIR)
	$(CC) $(CFLAGS) -c $< -o $@

$(BUILDDIR)/test_%.o: $(TESTDIR)/%.c | $(BUILDDIR)
	$(CC) $(CFLAGS) -c $< -o $@

# 创建构建目录
$(BUILDDIR):
	mkdir -p $(BUILDDIR)

# 清理
clean:
	rm -rf $(BUILDDIR)

# 运行
run: $(TARGET)
	./$(TARGET)

# 调试版本
debug: CFLAGS += -g -DDEBUG
debug: $(TARGET)

# 安装依赖（如果需要）
deps:
	@echo "No external dependencies required"

.PHONY: all clean test run debug deps
